<?php
/*
本系统由苏酷伊基于SiteHub项目二次开发
作者博客： https://www.sukuy.com/
后续更新地址： https://www.sukuy.com/navigation
作者QQ：3288637559
QQ交流群：884250547
任何第三方非官网在发布本系统时，不得删除该注释、版权信息、作者信息及官网等内容
*/
define('FROM_RESETPWD', true);
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
$original_act = isset($_GET['act']) ? $_GET['act'] : '';


$_GET['act'] = '';


include_once('email.php');


spl_autoload_register(function ($class) {
    
});
require_once('../includes/functions.php');


if ($original_act !== '') {
    $_GET['act'] = $original_act;
}


require('../includes/lang.class.php');

$act = _get('act');
if ($act == 'sendcode') {
    @header('Content-Type: application/json; charset=UTF-8');
    $qq = _post('qq');
    
    if (empty($qq)) {
        echo json_encode(array('code' => -1, 'msg' => 'QQ号码不能为空！'));
        exit;
    }
    
    
    if ($qq != $conf['qq']) {
        echo json_encode(array('code' => -2, 'msg' => 'QQ号码不正确！'));
        exit;
    }
    
    
    if(empty($conf['smtp_server']) || empty($conf['smtp_port']) || empty($conf['smtp_user']) || empty($conf['smtp_pass']) || empty($conf['smtp_from'])) {
        echo json_encode(array('code' => -3, 'msg' => '邮件服务器配置不完整，请先到系统设置中配置邮件服务器！'));
        exit;
    }
    
    
    $code = generate_verification_code(6);
    $_SESSION['reset_code'] = $code;
    $_SESSION['reset_time'] = time();
    
    
    $email = $qq . '@qq.com';
    $result = send_verification_email($email, $code);
    
    if ($result === true) {
        
        add_verify_log($email, $code, 1);
        echo json_encode(array('code' => 0, 'msg' => '验证码已发送至QQ邮箱：' . $email));
        exit;
    } else {
        
        error_log('重置密码验证码发送失败: ' . $result);
        
        add_verify_log($email, $code, 0, $result);
        echo json_encode(array('code' => -4, 'msg' => '邮件发送失败：' . $result));
        exit;
    }
} else if ($act == 'sendpassword') {
    @header('Content-Type: application/json; charset=UTF-8');
    $qq = _post('qq');
    $authcode = _post('authcode');
    $password = _post('password');
    
    if (empty($qq) || empty($authcode) || empty($password)) {
        echo json_encode(array('code' => -1, 'msg' => '所有字段都不能为空！'));
        exit;
    }
    
    
    if ($qq != $conf['qq']) {
        echo json_encode(array('code' => -2, 'msg' => 'QQ号码不正确！'));
        exit;
    }
    
    
    if (!isset($_SESSION['reset_code']) || $_SESSION['reset_code'] != $authcode || (time() - $_SESSION['reset_time'] > 300)) {
        echo json_encode(array('code' => -3, 'msg' => '验证码不正确或已过期！'));
        exit;
    }
    
    
    $new_password = hash('sha256', $password);
    $DB->query("UPDATE {$dbconfig['prefix']}_config SET v='$new_password' WHERE k='admin_pwd'");
    
    
    unset($_SESSION['reset_code'], $_SESSION['reset_time']);
    
    
    add_resetpwd_log($qq, 1, '', 'admin');
    
    echo json_encode(array('code' => 0, 'msg' => '密码修改成功，请使用新密码登录！'));
    exit;
}?>
<html>
<head>
    <title>找回密码 - 我爱导航系统</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="shortcut icon" href="../favicon.ico" />
    <link rel="stylesheet" href="<?php echo $site_cdnpublic; ?>twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
    <link rel="stylesheet" href="../assets/css/admin.css" />
    <link rel="stylesheet" href="../assets/css/custom_layer.css" />
    <style>
        /* 保留最基础的样式，确保页面功能正常 */
        html, body {
            height: 100%;
            margin: 0;
            padding: 0;
            background: #ffffff !important;
            overflow: hidden;
        }
        
        /* 密码重置相关样式 */
        .send-password {
            display: none;
        }
    </style>
</head>

<body>
    <!-- 重置密码功能已集成到登录页面弹窗中 -->
    <!-- 保留基本HTML结构以确保兼容性 -->
    <div style="display: flex; justify-content: center; align-items: center; height: 100vh;">
        <div style="text-align: center;">
            <h2>重置密码功能已集成到登录页面</h2>
            <p>请返回登录页面，点击"忘记密码"链接</p>
            <a href="login.php" class="btn btn-primary" style="margin-top: 20px;">返回登录</a>
        </div>
    </div>

    <script src="<?php echo $site_cdnpublic; ?>jquery/1.12.4/jquery.min.js"></script>
    <script src="<?php echo $site_cdnpublic; ?>twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>

</html>